package java44th.group.service.admin.admin;

import com.github.pagehelper.PageHelper;
import java44th.group.bean.admin.bo.admin.CreateBo;
import java44th.group.bean.admin.bo.admin.UpdateBo;
import java44th.group.bean.po.MarketAdmin;
import java44th.group.bean.admin.vo.BaseRespVo;
import java44th.group.bean.admin.vo.admin.*;
import java44th.group.mapper.MarketAdminMapper;
import java44th.group.utils.LocalHost;
import java44th.group.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;

/**
 * @Aurhor: 王可龙
 * @DATE: 2022/10/11 21:18
 * @Package:java44th.group.service.admin.admin
 * @Description:
 * @Version 1.0
 */
@Service
public class MarketAdminServiceImpl implements MarketAdminService {

    @Autowired
    MarketAdminMapper marketAdminMapper;


    @Override
    public BaseRespVo list(Integer page, Integer limit, String username, String sort, String order) {
        PageHelper.startPage(page, limit);
        LinkedList<MarketAdminVo> list = marketAdminMapper.selectAllData(username);
        LinkedList<MarketAdminVo2> list1 = new LinkedList<>();
        LinkedList<Integer> roleIds = new LinkedList<>();
        for (MarketAdminVo marketAdminVo : list) {
            String roleIds1 = marketAdminVo.getRoleIds();
            String replace = marketAdminVo.getRoleIds().replace("[", "");
            String replace2 = replace.replace("]", "");

            if (replace2.equals("")) {
                roleIds = new LinkedList<>();
            } else {
                String[] split = replace2.split(",");
                if (!split[0].equals("") && !split.equals("")) {
                    for (String s : split) {
                        if (s.equals("")){
                            roleIds=new LinkedList<>();
                            break;
                        }
                        Integer roleId = Integer.valueOf(s);
                        roleIds.add(roleId);
                    }
                }
            }
            MarketAdminVo2 marketAdminVo2 = new MarketAdminVo2(marketAdminVo.getId(), marketAdminVo.getUsername(), marketAdminVo.getAvatar(), roleIds);
            list1.add(marketAdminVo2);
            roleIds = new LinkedList<>();
        }
        Integer total = Math.toIntExact(marketAdminMapper.countByExample(null));
        Integer pages = total / limit;
        if (total % limit > 0) {
            pages++;
        }
        ListVo listVo = new ListVo(total, pages, limit, page, list1);
        return BaseRespVo.ok(listVo);
    }

    @Override
    public CreateVo create(CreateBo createBo) throws ParseException {
        Integer integer = marketAdminMapper.selectAdminByUsername(createBo.getUsername());
        if (integer != 0) {
            return null;
        }
        System.out.println(createBo);
        String replace;
        if (!StringUtil.isEmpty(createBo.getAvatar())) {
            replace = createBo.getAvatar().replace(LocalHost.localhost, "");
            createBo.setAvatar(replace);
        }
        String roleIds = null;
        if (createBo.getRoleIds().size() != 0) {
            roleIds = "[";
            for (Integer roleId : createBo.getRoleIds()) {
                if ("[".equals(roleIds)) {
                    roleIds = roleIds + roleId;
                    continue;
                }
                roleIds = roleIds + "," + roleId;
            }
            roleIds += "]";
        }
        LinkedList<Integer> roleIdsList = createBo.getRoleIds();
        Integer[] roleIdArray = new Integer[roleIdsList.size()];
        Integer[] realRoleIdArray = roleIdsList.toArray(roleIdArray);
        long l = System.currentTimeMillis();
        Date date = new Date(l);
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Timestamp ts = new Timestamp(l);
        MarketAdmin marketAdmin = new MarketAdmin(null, createBo.getUsername(), createBo.getPassword(), null, null, createBo.getAvatar(), ts, ts, null, realRoleIdArray);
        int i = marketAdminMapper.insertSelective(marketAdmin);
        String avatar;
        if (StringUtil.isEmpty(createBo.getAvatar())) {
            avatar = marketAdmin.getAvatar();
        } else {
            avatar = "http://localhost:8083/" + marketAdmin.getAvatar();
        }
        CreateVo createVo = new CreateVo(marketAdmin.getId(), marketAdmin.getUsername(), marketAdmin.getPassword(), avatar, sd.format(date), sd.format(date), createBo.getRoleIds());
        return createVo;
    }

    @Override
    public UpdateVO update(UpdateBo updateBo) {
        Integer integer = marketAdminMapper.selectAdminByUsername(updateBo.getUsername());
        String username = marketAdminMapper.selectUsernameById(updateBo.getId());
        if (!updateBo.getUsername().equals(username)) {
            return null;
        }
        String roleIds = null;
        if (updateBo.getRoleIds().size() != 0) {
            roleIds = "[";
            for (Integer roleId : updateBo.getRoleIds()) {
                if ("[".equals(roleIds)) {
                    roleIds = roleIds + roleId;
                    continue;
                }
                roleIds = roleIds + "," + roleId;
            }
            roleIds += "]";
        }
        long l = System.currentTimeMillis();
        Timestamp ts = new Timestamp(l);
        LinkedList<Integer> roleIdsList = updateBo.getRoleIds();
        Integer[] roleIdArray = new Integer[roleIdsList.size()];
        Integer[] realRoleIdArray = roleIdsList.toArray(roleIdArray);
        MarketAdmin marketAdmin = new MarketAdmin(updateBo.getId(), updateBo.getUsername(), updateBo.getPassword(), null, null, updateBo.getAvatar(), null, ts, null, realRoleIdArray);
        int i = marketAdminMapper.updateByPrimaryKeySelective(marketAdmin);
        Timestamp timestamp = marketAdminMapper.selectAddTimeById(updateBo.getId());
        UpdateVO updateVO = new UpdateVO(updateBo.getId(), updateBo.getUsername(), updateBo.getAvatar(), timestamp.toString(), ts.toString(), updateBo.getRoleIds());
        return updateVO;
    }

    @Override
    public void delete(UpdateBo updateBo) {
        // int i = marketAdminMapper.deleteByPrimaryKey(updateBo.getId());
        MarketAdmin marketAdmin = new MarketAdmin();
        marketAdmin.setId(updateBo.getId());
        marketAdmin.setDeleted(true);
        int i = marketAdminMapper.updateByPrimaryKeySelective(marketAdmin);
    }


}
